/*
 * claplan_controller.h
 *
 *  Created on: 1 Nov 2013
 *      Author: Tom Vercauteren
 */

#ifndef CLAPLAN_CONTROLLER_H_
#define CLAPLAN_CONTROLLER_H_

#include <string>
#include <vector>

#include "claplan_data.h"
#include "claplan_parser.h"
#include "claplan_data_change_listener.h"

class ClaplanController;
class ClaplanView;

/**
 * Controls the communication between the view and data model and implements the functionality of the view.
 */
class ClaplanController
{
public:
	ClaplanController();
	virtual ~ClaplanController();
	/**
	 * Creates a new claplan file
	 * @param file path to the claplan file
	 * @return
	 * <ul>
	 * 	<li>  0: file was succesfully created.</li>
	 * 	<li> <0: file was not succesfully created.</li>
	 * </ul>
	 */
	int newClaplanFile(const std::string &file);
	/**
	 * Opens a claplan file
	 * @param file path to the claplan file
	 * @return
	 * <ul>
	 * 	<li>  0: file was opened and parsed succesfully.</li>
	 * 	<li> <0: file was not opened or parsed succesfully.</li>
	 * </ul>
	 */
	int openClaplanFile(const std::string &file);

	/**
	 * Controller acquires a view and adds the controller to the view.
	 * @param view ClaplanView instance
	 */
	void setView(ClaplanView *view);
	/**
	 * Removes the controller from the view and removes the view.
	 */
	void removeView();
	/**
	 * Add a data model
	 * @param model ClaplanData model
	 */
	void addModel(ClaplanData* model);
	/**
	 * Removes a model from the controller.
	 * @param model ClaplanData model
	 */
	void removeModel(ClaplanData* model);
	/**
	 * Remove all data models and destroys their pointers;
	 */
	void removeModels();

	/**
	 * Returns the memory address of ClaplanData at the index location.
	 * @param index location
	 * @return Pointer to the ClaplanData (0 is returned when the index is out of bound).
	 */
	ClaplanData* at(unsigned index);
	/**
	 * Number of ClaplanData instances listed in the controller.
	 * @return Number of ClaplanData instances.
	 */
	unsigned size() const;
	/**
	 * Get a pointer to a ClaplanData list
	 * @return ClaplanData list
	 */
	ClaplanDataList* getModelList();

	void addClaplanDataListener(ClaplanDataChangeListener *listener);
	void removeClaplanDataListener(ClaplanDataChangeListener *listener);
	void dataChanged();

	void setProgramName(const std::string &name);
	void selectionTreeChanged(const std::string &claplan, const std::string &section);

private:
	/** GUI view */
	ClaplanView* view;
	/** File parser */
	ClaplanParser *parser;
	/** List of data models */
	ClaplanDataList modelList;
	std::vector<ClaplanDataChangeListener*> listeners;
	/** program name */
	std::string pname;
};

#endif /* CLAPLAN_CONTROLLER_H_ */
